package org.jpass.dc.view.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jpass.dc.business.project.logic.ZipService;
import org.jpass.dc.common.util.StringUtil;
import org.jpass.dc.view.common.BasicAction;

/**
 * 批量下载文件 方法是： 使用ant.jar包中的org.apache.tools.zip.*完成压缩之后再提供下载
 * 
 * @author Administrator
 * 
 */
public class BatchDownloadAction extends BasicAction {

	private static final long serialVersionUID = 6770036223469341642L;

	private Log log = LogFactory.getLog(BatchDownloadAction.class);
	
	private ZipService zipService = new ZipService();

	/* 需要下载文件的路径 */
	private String filePath;

	/* 需要下载的文件名称 */
	private String fileName;

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * @getFileName 此方法对应的是struts.xml文件中的： <param
	 *              name="contentDisposition">attachment
	 *              ;filename="${fileName}"</param> 这个属性设置的是下载工具下载文件时显示的文件名，
	 *              要想正确的显示中文文件名，我们需要对fileName再次编码 否则中文名文件将出现乱码，或无法下载的情况
	 */
	public String getFileName() throws UnsupportedEncodingException {
		String fileNameInCN = fileName + ".zip";
		try {
			fileNameInCN = new String(fileNameInCN.getBytes(), "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			log.error("下载文件\"" + fileName + "\"时转换字符编码出现异常，异常信息为：", e);
		}
		return fileNameInCN;
	}

	/**
	 * @getDownloadFile 此方法对应的是struts.xml文件中的： <param
	 *                  name="inputName">downloadFile
	 *                  </param>返回下载文件的流，可以参看struts2的源码
	 */
	public InputStream getDownloadFile() throws Exception {
		String realPath = this.getUserRealWrokRootPath() + "/" + fileName + ".zip";
		File file = new File(realPath);
		InputStream is = new FileInputStream(file);
		return is;
	}

	public String execute() {
		if (StringUtil.isBlank(filePath) || StringUtil.isBlank(fileName)) {
			return null;
		}

		try {
			String userRealWorkRootPath = this.getUserRealWrokRootPath();
			String zipFilePath = userRealWorkRootPath + "/" + fileName + ".zip";
			String srcFilePath = userRealWorkRootPath + "/" + filePath;

			zipService.compress(srcFilePath, zipFilePath); // 对需要下载的文件进行打包
		} catch (IOException e) {
			log.error("打包文件\"" + fileName + "\"时出现异常，异常信息为：", e);
		}
		return SUCCESS;
	}

	/*public void download() {
		if (StringUtil.isBlank(filePath) || StringUtil.isBlank(fileName)) {

		}

		String userRealWorkRootPath = this.getUserRealWrokPath();
		String zipFilePath = userRealWorkRootPath + "/" + fileName + ".zip";
		String srcFilePath = userRealWorkRootPath + "/" + filePath;
		try {
			new ZipService().compress(srcFilePath, zipFilePath); // 对需要下载的文件进行打包
			HttpServletResponse response = ServletActionContext.getResponse();
			InputStream ins = new FileInputStream(zipFilePath);
			BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面
			OutputStream outs = response.getOutputStream();// 获取文件输出IO流
			BufferedOutputStream bouts = new BufferedOutputStream(outs);
			response.setContentType("application/x-download");// 设置response内容的类型
			response.setHeader("Content-disposition", "attachment;filename="
					+ URLEncoder.encode(fileName + ".zip", "UTF-8"));// 设置头部信息
			int len = 0;
			byte[] buffer = new byte[4096]; // 开始向网络传输文件流
			while ((len = bins.read(buffer, 0, 8192)) != -1) {
				bouts.write(buffer, 0, len);
			}
			bouts.flush();// 这里一定要调用flush()方法 bins.close(); ins.close();
			bouts.close();
			outs.close();
		} catch (IOException e) {
			log.error("批量下载\"" + userRealWorkRootPath + "/" + filePath
					+ "\"文件时时发生异常，异常信息：", e);
		}
	}*/
}
